import conDb from "./index";

export default {
  addUser({type = 1, tel, cookie, uin}){
    let db = conDb();

    let createdTimer = new Date().getTime();

    return new Promise((resolve, reject) => {
        let sql = `INSERT INTO test ( tel, cookie, createdTimer, uin) `;
        sql += `values ("${tel}", "${cookie}", "${createdTimer}", "${uin}")`;

        let inquire = `select * from test where tel = "${tel}"`;
        
        db.all(inquire,(err,list) => { // 查询用户
            if (err) {
                reject({code:400,msg:err,data:[]});
            }else {
                if (list.length) { // 有用户
                    resolve({code:201,msg:'已有该用户',data:list})
                }else { // 没有用户
                    db.all(sql,(error,data) => {
                        if (error) {
                          reject({code:400,msg:error});
                        } else {
                          resolve({code:200,msg:'成功',data});
                        }
                    });
                }
            }
        })
    })
  }
}